<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage_account extends CI_Controller {

    public $session_data;

    public function __construct()
    {
        parent::__construct();
        //Not Completed
        $this->load->model('info_model');
        $this->load->model('manage_user_model');
        $this->load->helper("url");
        $this->load->library("pagination");

        $this->session_data = $this->session->userdata('logged_in');
    }

    public function manage_user()
    {

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $this->check_user();

        $data['owner_id'] = $this->session_data['user_id'];

        $this->load->helper('form');
        $this->load->library("pagination");
        $data['title'] = "Manage Account";

        $session_data = $this->session->userdata('logged_in');

        $config["base_url"] = base_url()."index.php/manage_account/manage_user";
        $config["total_rows"] = 3;
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = $this->uri->segment(3);

        $result = $this->info_model->check_acc($session_data['user_id']);
        $data['user_account'] = $this->manage_user_model->display_user_account($config["per_page"], $page, $result->user_id);

        //echo var_dump($data['user_account']);

        $data['test'] = $result;
        $data['fname'] = $result->fname;
        $data['lname'] = $result->lname;
        $data['user_id'] = $result->user_id;

        $data["results"] = $this->manage_user_model->display_user_account_($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();

        $data['college'] = $this->manage_user_model->display_user_college();

        $data['type'] = $this->manage_user_model->display_user_type();

        $session_data = $this->session->userdata('logged_in');
        $data['typeh'] = $session_data['type_no'];

        $this->load->view('template/header', $data);
        $this->load->view('pages/manage_accounts',$data);
        $this->load->view('template/footer');

    }

    public function add_user()
    {

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $pass = $this->input->post('pass');
        $session_data = $this->session->userdata('logged_in');
        $type = $session_data['type_no'];
        $college = $session_data['coll_id'];
        $coll_id = (int)$college;
        //echo var_dump($pass);

        $this->load->library('form_validation');

        $this->form_validation->set_rules('user', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('pass', 'Password', 'trim|xss_clean');
        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('middle_initial', 'Middle Initial', 'trim|required|xss_clean');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');
        if($type != 1){
            $this->form_validation->set_rules('college', 'College', 'trim|required|xss_clean');
        }

        if ($this->form_validation->run() === FALSE)
        {

            redirect('manage_account/manage_user','refresh');

        }

        else
        {

            if($pass == ""){
                //echo "WHYYYYY?????";
                $new = array(
                    "user_id" => $this->input->post('user'),
                    "type_no" => $this->input->post('type'),
                    "coll_id" => $coll_id
                );

            }
            else{

                echo "This one entered";
                $new = array(
                    "user_id" => $this->input->post('user'),
                    "password" => $this->input->post('pass'),
                    "type_no" => $this->input->post('type'),
                    "coll_id" => $coll_id
                );

            }

            $new2 = array(
                "user_id" => $this->input->post('user'),
                "fname" => $this->input->post('fname'),
                "middle_initial" => $this->input->post('middle_initial'),
                "lname" => $this->input->post('lname')
            );

            $new3 = array(
                "user_id" => $this->input->post('user'),
            );

            $this->manage_user_model->insert_data($new, $new2, $new3);

            redirect('manage_account/manage_user','refresh');

        }

    }

    public function delete_user($user){

        //This function should check if the user is one level higher than the chosen user so that they can delete them.

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $this->manage_user_model->delete_data($user);

        redirect('manage_account/manage_user','refresh');

    }

    function check_user ()
    {

        $session_data = $this->session->userdata('logged_in');

        $type = $session_data['type_no'];
        $user_id = $session_data['user_id'];
        $coll_id = $session_data['coll_id'];

        if( !($type > 0 and $type < 2) )
            redirect('announcement/view/', 'refresh');

        //echo var_dump($session_data);
        //echo var_dump($result);
        //echo var_dump($result['coll_id'] == $session_data['coll_id']);

    }

    public function logout()
    {

        $this->session->unset_userdata('logged_in');
        redirect('welcome/home', 'refresh');

    }

}

/* End of file manage_account.php */
/* Location: ./application/controllers/manage_account.php */